package com.ywl.handler;

import com.alibaba.fastjson.JSON;
import com.ywl.common.ResponseResult;
import com.ywl.utlis.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 24040
 * @name AuthenticationEntryPointImpl
 * @date 2022-10-20 22:47
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {


    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED.value(),"用户认证失败请查询登陆");
        String strJson = JSON.toJSONString(result);
        //处理异常:无论是授权失败还是响应失败了都希望能够响应回去一个json格式，responseEntity
        WebUtils.renderString(response,strJson);
    }
}
